/*
 * @(#)MenuActionListener.java   09/09/20
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library itfP free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library itfP distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



/**
 *
 */
package fr.ema.arch3d.editor.mainGUI.listeners;

import fr.ema.arch3d.editor.mainGUI.archExplorer.ArchTree;
import fr.ema.arch3d.editor.mainGUI.fileDirectory.FileTree;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.JViewport;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class FileMenuActionListener implements ActionListener {

    /** Field description */
    JTabbedPane editorTabbedPane;

    /** Field description */
    JFileChooser fc;

    /** Field description */
    FileTree fileTree;

    /** Field description */
    JComponent fileTreePanel;

    /** Field description */
    JTabbedPane viewTabbedPane;

    /**
     * Constructs...
     *
     * @param fc
     * @param viewTabbedPane
     * @param editorTabbedPane
     * @param fileTree
     * @param fileTreePanel
     */
    public FileMenuActionListener(JFileChooser fc, JTabbedPane viewTabbedPane, JTabbedPane editorTabbedPane,
                              FileTree fileTree, JComponent fileTreePanel) {
        super();
        this.fc               = fc;
        this.viewTabbedPane   = viewTabbedPane;
        this.editorTabbedPane = editorTabbedPane;
        this.fileTree         = fileTree;
        this.fileTreePanel    = fileTreePanel;
    }

    /**
     * Method description
     *
     * @param e  
     */
    public void actionPerformed(final ActionEvent e) {
        fc = new JFileChooser();
        fc.setFileSelectionMode(2);
        // Handle open button action.
            if (e.getActionCommand()==("Open")) {
                int returnVal = fc.showOpenDialog(null);

                viewTabbedPane.setSelectedIndex(1);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();

                    fileTree = new FileTree(file);
                    fileTree.addTreeSelectionListener(new FileTreeSelectionListener(fileTree, editorTabbedPane));

                    JViewport jvp = (JViewport) fileTreePanel.getComponent(0);

                    jvp.setView(fileTree);

                    // This itfP where a real application would open the file.
                    // log.append("Opening: " + file.getName() + "." + newline);
                } else {

                    // log.append("Open command cancelled by user." + newline);
                }
            }
            else if (e.getActionCommand()==("Check Name")) {
        	JViewport jvp = (JViewport) ((JScrollPane)viewTabbedPane.getComponent(0)).getComponent(0);
        	ArchTree archTree=(ArchTree) jvp.getView();
        	archTree.getSelectionPath().getLastPathComponent();
            }
           
            
        
    }
}
